package c.x.jy.service.service.impl;

import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.MemberWalletDao;
import c.x.jy.dao.dto.MemberWalletEntityDto;
import c.x.jy.dao.entity.MemberWalletEntity;
import c.x.jy.service.service.MemberService;
import c.x.jy.service.service.MemberWalletService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberWalletService")
public class MemberWalletServiceImpl extends ServiceImpl<MemberWalletDao, MemberWalletEntity> implements MemberWalletService {

	
	@Autowired
	private MemberService memberService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<MemberWalletEntity> query= 	 new QueryWrapper<MemberWalletEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<MemberWalletEntity> page = this.page(
                new Query<MemberWalletEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @Override
    public MemberWalletEntity getByMemberId(Long memberId) {
        if(memberId==null)return null;
        return baseMapper.selectOne(new QueryWrapper<MemberWalletEntity>().lambda().eq(MemberWalletEntity::getMemberId,memberId).last("limit 1"));
    }

    @Override
    public MemberWalletEntity selectMemberWalletByIdFOrUpdate(Long id) {
        QueryWrapper<MemberWalletEntity> query= new QueryWrapper<>();
          query .lambda().eq(MemberWalletEntity::getMemberId,id);
        query.last("limit 1");

        MemberWalletEntity memberWalletEntity=    baseMapper.selectOne(query);

        return baseMapper.selectMemberWalletByIdFOrUpdate(memberWalletEntity.getId());
    }

	@Override
	public MemberWalletEntity selectMemberWalletByMemberIdFOrUpdate(Long memberId) {
	 
        return baseMapper.selectMemberWalletByIdFOrUpdate(getByMemberId(memberId).getId());
	}

    @Override
    public MemberWalletEntity getByCurUser() {
        Long memberId = memberService.getCurrUser().getId();

        return baseMapper.selectOne(new QueryWrapper<MemberWalletEntity>().lambda().eq(MemberWalletEntity::getMemberId,memberId).last("limit 1"));
    }

    @Override
    public PageUtils queryPageforWeb(Map<String, Object> params) {
        QueryWrapper<MemberWalletEntity> query= 	 new QueryWrapper<MemberWalletEntity>() ;
        if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
            query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<MemberWalletEntityDto> page = this.getBaseMapper().queryPageforWeb(
                new Query<MemberWalletEntity>().getPage(params),
                query
        );

        return new PageUtils(page);
    }
}
